//
//  CZTabBar.m
//  12期微博
//
//  Created by apple on 15-1-28.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "LWTabBarCustom.h"
#import "LWTabBarBtn.h"

@interface LWTabBarCustom()
/**
 *  加号按钮
 */
@property (nonatomic, weak)  UIButton *plusBtn;
@property (nonatomic,weak) LWTabBarBtn *selectBtn;
@property (nonatomic,strong) NSMutableArray *btns;
@end

@implementation LWTabBarCustom
#pragma mark - 懒加载
- (NSMutableArray *)btns{

    if (_btns == nil) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}


// 通过代码创建控件时会调用
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

// 通过xib/Storboard创建时调用
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup
{
    // 创建一个加号按钮, 添加到当前控件中
    // 1.创建按钮
    UIButton *plusBtn = [[UIButton alloc] init];
    plusBtn.tag = 998;
    // 2.设置背景图片
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    // 3.设置图标
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    // 4.添加
    [self addSubview:plusBtn];
    [plusBtn addTarget:self action:@selector(plusBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.plusBtn = plusBtn;
}
- (void)plusBtnClick:(UIButton *)plusBtn{

    if ([self.delegata respondsToSelector:@selector(tabBarCustom:selectplusBtn:)]) {
        [self.delegata tabBarCustom:self selectplusBtn:plusBtn];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 设置其他选项卡按钮的frame
    for (int i = 0; i < self.btns.count; i++) {
        
        LWTabBarBtn *btn = self.btns[i];
        
        CGFloat W = self.frame.size.width / self.subviews.count;
        CGFloat H = self.frame.size.height;
        CGFloat Y = 0;
        CGFloat X = i * W;
        if (i > 1) {
            X += W;
        }
        btn.frame = CGRectMake(X, Y, W, H);
    }
        
    
    self.plusBtn.size = self.plusBtn.currentBackgroundImage.size;
    self.plusBtn.centerX = self.width * 0.5;
    self.plusBtn.centerY = self.height * 0.5;
    
}
- (void)addItme:(UITabBarItem *)item{

    LWTabBarBtn *tabBtn = [[LWTabBarBtn alloc] init];
    tabBtn.itme = item;
    
    [self addSubview:tabBtn];
    [tabBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
    //绑定按钮的tag
    tabBtn.tag = self.btns.count;
    [self.btns addObject:tabBtn];
    if (self.btns.count == 1) {
        [self btnClick:tabBtn];
    }
    
}
- (void)btnClick:(LWTabBarBtn *)btn{

    if ([self.delegata respondsToSelector:@selector(tabBarCustom:from:to:)]) {
        [self.delegata tabBarCustom:self from:self.selectBtn.tag to:btn.tag];
    }
        
    self.selectBtn.selected = NO;
    btn.selected = YES;
    self.selectBtn = btn;
    //点击按钮,按钮变大或变小
    [UIView animateWithDuration:0.1 animations:^{
        btn.transform = CGAffineTransformMakeScale(0.5, 0.5);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            btn.transform = CGAffineTransformMakeScale(1.5, 1.5);
        } completion:^(BOOL finished) {
            btn.transform = CGAffineTransformIdentity;
        }];
    }];
    
    
    
    
    
}

@end
